/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.movieSystem.view;



import java.awt.*;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JOptionPane;

import com.movieSystem.model.UserInfo;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.DialogEvent;
import tcpFirmware.OKClient;
import tcpFirmware.Response;

/**
 *
 * @author DELL-PC
 */
public class FrmBalance extends javax.swing.JFrame {
    String username;
    public FrmBalance self;
    FrmUser user;
    /**
     * Creates new form FrmBalance
     */
    public FrmBalance(FrmUser us) {
        user=us;
        initComponents();
        this.username= UserInfo.getUsername();
        PnlMoney.setVisible(false);
        TextBalance.setEditable(false);
        shuaXin();
        setAlwaysOnTop(true);
    }

    private void alert(String text){
        JOptionPane.showMessageDialog(this,text,"系统提示",JOptionPane.INFORMATION_MESSAGE);
    }
    public void shuaXin() {
        try {
            OKClient okClient=new OKClient(new Response() {

                @Override
                public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
                    // TODO Auto-generated method stub
                    HashMap<String, String> data = OKClient.dealWithForm(dataBytes);
                    if(header.get("查询结果").equals("查询成功")) {
                        alert("查询成功");
                    }else {
                        alert("查询失败");
                    }
                    TextBalance.setText(data.get("余额"));
                }
            });
            okClient.setType("text/formData").setRoute("balance_query").setData("username", username);
            okClient.sendTo();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabelBalance = new javax.swing.JLabel();
        TextBalance = new javax.swing.JTextField();
        BtnInvestMoney = new javax.swing.JButton();
        BtnReturn = new javax.swing.JButton();
        PnlMoney = new javax.swing.JPanel();
        TextMoney = new javax.swing.JTextField();
        BtnConfirm = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("余额查询");

        LabelBalance.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
        LabelBalance.setText("余额");

        TextBalance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextBalanceActionPerformed(evt);
            }
        });

        BtnInvestMoney.setFont(new java.awt.Font("宋体", 1, 48)); // NOI18N
        BtnInvestMoney.setForeground(new java.awt.Color(255, 204, 0));
        BtnInvestMoney.setText("氪金");
        BtnInvestMoney.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnInvestMoneyActionPerformed(evt);
            }
        });

        BtnReturn.setText("返回");
        BtnReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnReturnActionPerformed(evt);
            }
        });

        TextMoney.setText("请输入充值面额");
        TextMoney.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextMoneyActionPerformed(evt);
            }
        });

        BtnConfirm.setText("确认充值");
        BtnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnConfirmActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PnlMoneyLayout = new javax.swing.GroupLayout(PnlMoney);
        PnlMoney.setLayout(PnlMoneyLayout);
        PnlMoneyLayout.setHorizontalGroup(
                PnlMoneyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PnlMoneyLayout.createSequentialGroup()
                                .addComponent(TextMoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(BtnConfirm))
        );
        PnlMoneyLayout.setVerticalGroup(
                PnlMoneyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(PnlMoneyLayout.createSequentialGroup()
                                .addGap(0, 136, Short.MAX_VALUE)
                                .addGroup(PnlMoneyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(TextMoney, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(BtnConfirm, javax.swing.GroupLayout.Alignment.TRAILING)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(BtnInvestMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(BtnReturn)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addComponent(LabelBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(TextBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addComponent(PnlMoney, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(0, 99, Short.MAX_VALUE))
                        ));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(67, 67, 67)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(LabelBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(TextBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(54, 54, 54)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(BtnInvestMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(BtnReturn)
                                                .addGap(9, 9, 9)))
                                .addGap(49, 49, 49)
                                .addComponent(PnlMoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(69, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void BtnInvestMoneyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnInvestMoneyActionPerformed
        // TODO add your handling code here:
        PnlMoney.setVisible(true);
    }//GEN-LAST:event_BtnInvestMoneyActionPerformed

    private void TextBalanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextBalanceActionPerformed

    }//GEN-LAST:event_TextBalanceActionPerformed

    private void BtnReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnReturnActionPerformed
        dispose();
    }//GEN-LAST:event_BtnReturnActionPerformed

    private void TextMoneyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextMoneyActionPerformed

    }//GEN-LAST:event_TextMoneyActionPerformed

    private void BtnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnConfirmActionPerformed
        try {
            OKClient okClient=new OKClient(new Response() {

                @Override
                public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
                    // TODO Auto-generated method stub
                    HashMap<String, String> data = OKClient.dealWithForm(dataBytes);
                    if(header.get("充值状态").equals("充值成功")) {
                        alert("充值成功");
                        shuaXin();
                    }else {
                        alert("充值失败");
                    }
                }
            });
            okClient.setType("text/formData").setRoute("balance_invest").setData("username", username).setData("money",TextMoney.getText().toString());
            okClient.sendTo();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }//GEN-LAST:event_BtnConfirmActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmBalance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmBalance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmBalance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmBalance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnConfirm;
    private javax.swing.JButton BtnInvestMoney;
    private javax.swing.JButton BtnReturn;
    private javax.swing.JLabel LabelBalance;
    private javax.swing.JPanel PnlMoney;
    private javax.swing.JTextField TextBalance;
    private javax.swing.JTextField TextMoney;
    // End of variables declaration//GEN-END:variables
}
